package com.unswift.window.factory;

import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.FocusEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.unswift.QuickApplication;
import com.unswift.constant.ButtonConstant;
import com.unswift.constant.QuickConstant;
import com.unswift.generics.project.AddFieldRule;
import com.unswift.generics.project.DropFieldRule;
import com.unswift.generics.project.ModuleRule;
import com.unswift.generics.project.ProjectConfig;
import com.unswift.generics.project.ProjectFactory;
import com.unswift.generics.project.ServerRule;
import com.unswift.utils.DateUtils;
import com.unswift.utils.FileUtils;
import com.unswift.utils.NumberUtils;
import com.unswift.utils.ObjectUtils;
import com.unswift.utils.StringUtils;
import com.unswift.window.dialog.ConnectionDatabaseDialog;
import com.unswift.window.dialog.GenericsPathConfigDialog;
import com.unswift.window.extend.Attribute;
import com.unswift.window.extend.Button;
import com.unswift.window.listener.ButtonActionListener;
import com.unswift.window.listener.DialogButtonAction;
import com.unswift.window.table.Table;

public final class ButtonFactory {
	
	public static JButton createDialogButton(Attribute attribute){
		String name=attribute.get(Attribute.ATTRIBUTE_BUTTON_NAME);
		JButton button=ObjectUtils.isEmpty(name)?new JButton():new JButton(name);
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_ICON)){
			button.setIcon(attribute.get(Attribute.ATTRIBUTE_BUTTON_ICON));
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_OPAQUE)){
			boolean isOpaque = attribute.get(Attribute.ATTRIBUTE_BUTTON_OPAQUE);
			button.setOpaque(isOpaque);
			button.setContentAreaFilled(!isOpaque);
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_ENABLED)){
			button.setEnabled(attribute.get(Attribute.ATTRIBUTE_BUTTON_ENABLED));
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_ACTION)){
			button.setActionCommand(attribute.get(Attribute.ATTRIBUTE_BUTTON_ACTION));
		}
		button.addActionListener(new DialogButtonAction(attribute.get(Attribute.ATTRIBUTE_BUTTON_DEPEND)));
		return button;
	}
	public static JButton createButton(Attribute attribute){
		String name=attribute.get(Attribute.ATTRIBUTE_BUTTON_NAME);
		JButton button;
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_ORIGINAL)){
			if((boolean)attribute.get(Attribute.ATTRIBUTE_BUTTON_ORIGINAL)){
				button=ObjectUtils.isEmpty(name)?new JButton():new JButton(name);
			}else{
				button=ObjectUtils.isEmpty(name)?new Button():new Button(name);
			}
		}else{
			button=ObjectUtils.isEmpty(name)?new Button():new Button(name);
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_ICON)){
			button.setIcon(attribute.get(Attribute.ATTRIBUTE_BUTTON_ICON));
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_TIPS)){
			button.setToolTipText(attribute.get(Attribute.ATTRIBUTE_BUTTON_TIPS));
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_OPAQUE)){
			boolean isOpaque = attribute.get(Attribute.ATTRIBUTE_BUTTON_OPAQUE);
			button.setOpaque(isOpaque);
			button.setContentAreaFilled(isOpaque);
		}else{
			button.setContentAreaFilled(false);
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_ENABLED)){
			button.setEnabled(attribute.get(Attribute.ATTRIBUTE_BUTTON_ENABLED));
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_ACTION)){
			button.setActionCommand(attribute.get(Attribute.ATTRIBUTE_BUTTON_ACTION));
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_MARGIN)){
			Insets margin = attribute.get(Attribute.ATTRIBUTE_BUTTON_MARGIN);
			if(ObjectUtils.isNotEmpty(margin)) {
				button.setMargin(margin);
			}
		}else{
			button.setMargin(new Insets(1, 2, 1, 2));
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_SIZE)){
			button.setPreferredSize(attribute.get(Attribute.ATTRIBUTE_BUTTON_SIZE));
		}
		button.setBackground(Color.WHITE);
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_BGCOLOR)){
			button.setBackground(attribute.get(Attribute.ATTRIBUTE_BUTTON_BGCOLOR));
		}
		if(button instanceof Button){
			((Button) button).setInitBgColor(button.getBackground());
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_PADDING)){
			Insets padding=attribute.get(Attribute.ATTRIBUTE_BUTTON_PADDING);
			if(ObjectUtils.isNotEmpty(padding)){
				if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_BGCOLOR)){
					Color color=attribute.get(Attribute.ATTRIBUTE_BUTTON_BGCOLOR);
					if(ObjectUtils.isNull(color)){
						button.setBorder(BorderFactory.createEmptyBorder(padding.top, padding.left, padding.bottom, padding.right));
					}else{
						button.setBorder(BorderFactory.createMatteBorder(padding.top, padding.left, padding.bottom, padding.right, color));
					}
				}else{
					button.setBorder(BorderFactory.createMatteBorder(padding.top, padding.left, padding.bottom, padding.right, Color.WHITE));
				}
			}
		}else{
			button.setBorder(BorderFactory.createMatteBorder(1, 2, 1, 2, Color.WHITE));
		}
		Object comps=attribute.get(Attribute.ATTRIBUTE_BUTTON_DEPEND);
		if(ObjectUtils.isNotEmpty(comps) && comps.getClass().isArray()){
			button.addActionListener(new ButtonActionListener((Object[])comps));
		}else{
			if(ObjectUtils.isEmpty(comps)){
				button.addActionListener(new ButtonActionListener());
			}else{
				button.addActionListener(new ButtonActionListener(comps));
			}
		}
		if(attribute.exists(Attribute.ATTRIBUTE_BUTTON_FOCUS)){
			button.dispatchEvent(new FocusEvent(button,FocusEvent.FOCUS_GAINED, attribute.get(Attribute.ATTRIBUTE_BUTTON_FOCUS)));
		}
		return button;
	}
	
	public static void executeButton(JButton button, String action, Object...comps){
		if(action.equals(ButtonConstant.ACTION_CREATE_PROJECT_PATH_FILE_SELECT)){
			fileSelect(comps);
			initProjectPath();
		}else if(action.equals(ButtonConstant.ACTION_CREATE_PROJECT)){
			genericsProject();
		}else if(action.equals(ButtonConstant.ACTION_CREATE_SERVER)){
			genericsServer();
		}else if(action.equals(ButtonConstant.ACTION_CONNECTION_DATABASE)){
			new ConnectionDatabaseDialog(QuickApplication.getWindow(), QuickConstant.CACHE_MODULE_DATABASE);
		}else if(action.equals(ButtonConstant.ACTION_CONNECTION_DATABASE_ADD_FIELD)){
			new ConnectionDatabaseDialog(QuickApplication.getWindow(), QuickConstant.CACHE_ADD_FIELD_DATABASE);
		}else if(action.equals(ButtonConstant.ACTION_CONNECTION_DATABASE_DROP_FIELD)){
			new ConnectionDatabaseDialog(QuickApplication.getWindow(), QuickConstant.CACHE_DROP_FIELD_DATABASE);
		}else if(action.equals(ButtonConstant.ACTION_GENERICS_PATH_CONFIG)){
			new GenericsPathConfigDialog(QuickApplication.getWindow());
		}else if(action.equals(ButtonConstant.ACTION_GENERICS_MODULE)){
			genericsModule();
		}else if(action.equals(ButtonConstant.ACTION_ADD_FIELD)){
			addField();
		}else if(action.equals(ButtonConstant.ACTION_DROP_FIELD)){
			dropField();
		}
	}
	
	private static void dropField() {
		String projectPath = FileChooseFactory.getDefaultFile().getAbsolutePath()+"\\";
		JTextField projectDomain=QuickApplication.cache.getHash("dropField", "projectDomain");
		String projectDomainText=projectDomain.getText();
		if(ObjectUtils.isEmpty(projectDomainText)){
			MessageFactory.error(QuickApplication.getWindow(), "域名不能为空");
			return ;
		}
		JTextField pojoName=QuickApplication.cache.getHash("dropField", "pojoName");
		String pojoNameText=pojoName.getText();
		if(ObjectUtils.isEmpty(pojoNameText)){
			MessageFactory.error(QuickApplication.getWindow(), "pojo路径不能为空");
			return ;
		}
		JTextField mapperName=QuickApplication.cache.getHash("dropField", "mapperName");
		String mapperNameText=mapperName.getText();
		if(ObjectUtils.isEmpty(mapperNameText)){
			MessageFactory.error(QuickApplication.getWindow(), "mapper路径不能为空");
			return ;
		}
		File file=new File(projectPath+pojoNameText);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Pojo路径“"+(projectPath+pojoNameText)+"”不存在,生成失败");
			return ;	
		}
		file=new File(projectPath+mapperNameText);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Mapper路径“"+(projectPath+mapperNameText)+"”不存在,生成失败");
			return ;	
		}
		
		JComboBox<String> databaseSelect=QuickApplication.cache.getHash("dropField", "database");
		String database=(String)databaseSelect.getSelectedItem();
		if(ObjectUtils.isEmpty(database)){
			MessageFactory.error(QuickApplication.getWindow(), "未选定数据库");
			return ;
		}
		JComboBox<String> tableSelect=QuickApplication.cache.getHash("dropField", "table");
		String tableName=(String)tableSelect.getSelectedItem();
		if(ObjectUtils.isEmpty(tableName)){
			MessageFactory.error(QuickApplication.getWindow(), "未选定表");
			return ;
		}else {
			tableName=tableName.substring(0, tableName.indexOf("("));
		}
		JTextField fieldName=QuickApplication.cache.getHash("dropField", "fieldName");
		String fieldNameText=fieldName.getText();
		if(ObjectUtils.isEmpty(fieldNameText)){
			MessageFactory.error(QuickApplication.getWindow(), "字段名称不能为空");
			return ;
		}
		JTextField module=QuickApplication.cache.getHash("dropField", "module");
		String moduleName=module.getText();
		if(ObjectUtils.isEmpty(moduleName)){
			MessageFactory.error(QuickApplication.getWindow(), "所属模块不能为空");
			return ;
		}
		JTextField currModule=QuickApplication.cache.getHash("dropField", "currModule");
		String currModuleName=currModule.getText();
		
		DropFieldRule rule=new DropFieldRule();
		rule.setProjectPath(projectPath);
		rule.setServerPojo(pojoNameText);
		rule.setServerMapper(mapperNameText);
		rule.setDatabase(database);
		rule.setTable(tableName);
		rule.setModule(moduleName);
		rule.setCurrModule(currModuleName);
		rule.setFieldName(fieldNameText);
		rule.setBasePackage(projectDomainText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_DOMAIN, projectDomainText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_POJO, pojoNameText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_MAPPER, mapperNameText);
		try {
			String addFieldInfo=ProjectFactory.dropField(rule);
			MessageFactory.info(QuickApplication.getWindow(), addFieldInfo);
		} catch (Exception e) {
			e.printStackTrace();
			MessageFactory.error(QuickApplication.getWindow(), "生成异常，消息："+e.getMessage());
		}
	}
	
	private static void addField() {
		String projectPath = FileChooseFactory.getDefaultFile().getAbsolutePath()+"\\";
		JTextField projectDomain=QuickApplication.cache.getHash("addField", "projectDomain");
		String projectDomainText=projectDomain.getText();
		if(ObjectUtils.isEmpty(projectDomainText)){
			MessageFactory.error(QuickApplication.getWindow(), "域名不能为空");
			return ;
		}
		JTextField pojoName=QuickApplication.cache.getHash("addField", "pojoName");
		String pojoNameText=pojoName.getText();
		if(ObjectUtils.isEmpty(pojoNameText)){
			MessageFactory.error(QuickApplication.getWindow(), "pojo路径不能为空");
			return ;
		}
		JTextField mapperName=QuickApplication.cache.getHash("addField", "mapperName");
		String mapperNameText=mapperName.getText();
		if(ObjectUtils.isEmpty(mapperNameText)){
			MessageFactory.error(QuickApplication.getWindow(), "mapper路径不能为空");
			return ;
		}
		File file=new File(projectPath+pojoNameText);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Pojo路径“"+(projectPath+pojoNameText)+"”不存在,生成失败");
			return ;	
		}
		file=new File(projectPath+mapperNameText);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Mapper路径“"+(projectPath+mapperNameText)+"”不存在,生成失败");
			return ;	
		}
		
		JComboBox<String> databaseSelect=QuickApplication.cache.getHash("addField", "database");
		String database=(String)databaseSelect.getSelectedItem();
		if(ObjectUtils.isEmpty(database)){
			MessageFactory.error(QuickApplication.getWindow(), "未选定数据库");
			return ;
		}
		JComboBox<String> tableSelect=QuickApplication.cache.getHash("addField", "table");
		String tableName=(String)tableSelect.getSelectedItem();
		if(ObjectUtils.isEmpty(tableName)){
			MessageFactory.error(QuickApplication.getWindow(), "未选定表");
			return ;
		}else {
			tableName=tableName.substring(0, tableName.indexOf("("));
		}
		JTextField fieldName=QuickApplication.cache.getHash("addField", "fieldName");
		String fieldNameText=fieldName.getText();
		if(ObjectUtils.isEmpty(fieldNameText)){
			MessageFactory.error(QuickApplication.getWindow(), "字段名称不能为空");
			return ;
		}
		JTextField fieldComment=QuickApplication.cache.getHash("addField", "fieldComment");
		String fieldCommentText=fieldComment.getText();
		if(ObjectUtils.isEmpty(fieldCommentText)){
			MessageFactory.error(QuickApplication.getWindow(), "字段描述不能为空");
			return ;
		}
		JTextField module=QuickApplication.cache.getHash("addField", "module");
		String moduleName=module.getText();
		if(ObjectUtils.isEmpty(moduleName)){
			MessageFactory.error(QuickApplication.getWindow(), "所属模块不能为空");
			return ;
		}
		JTextField currModule=QuickApplication.cache.getHash("addField", "currModule");
		String currModuleName=currModule.getText();
		JComboBox<String> fieldType=QuickApplication.cache.getHash("addField", "fieldType");
		String fieldTypeText=(String)fieldType.getSelectedItem();
		if(ObjectUtils.isEmpty(fieldTypeText)){
			MessageFactory.error(QuickApplication.getWindow(), "字段类型不能为空");
			return ;
		}
		JTextField fieldLength=QuickApplication.cache.getHash("addField", "fieldLength");
		String fieldLengthText=fieldLength.getText();
		if(fieldLength.isEditable() && ObjectUtils.isEmpty(fieldLengthText)){
			MessageFactory.error(QuickApplication.getWindow(), "字段长度不能为空");
			return ;
		}
		if(!NumberUtils.isUnsignedNumber(fieldLengthText, true)) {
			MessageFactory.error(QuickApplication.getWindow(), "字段长度只能是正整数");
			return ;
		}
		JTextField fieldScale=QuickApplication.cache.getHash("addField", "fieldScale");
		String fieldScaleText=fieldScale.getText();
		if(fieldScale.isEditable() && ObjectUtils.isEmpty(fieldScaleText)){
			MessageFactory.error(QuickApplication.getWindow(), "字段精度不能为空");
			return ;
		}
		if(!NumberUtils.isUnsignedNumber(fieldScaleText, true)) {
			MessageFactory.error(QuickApplication.getWindow(), "字段精度只能是正整数");
			return ;
		}
		JTextField fieldValue=QuickApplication.cache.getHash("addField", "fieldValue");
		String fieldValueText=fieldValue.getText();
		if(fieldValue.isEditable() && ObjectUtils.isEmpty(fieldValueText)){
			MessageFactory.error(QuickApplication.getWindow(), "字段枚举值不能为空");
			return ;
		}
		JTextField fieldDefault=QuickApplication.cache.getHash("addField", "fieldDefault");
		String fieldDefaultText=fieldDefault.getText();
		JTextField insertAfter=QuickApplication.cache.getHash("addField", "insertAfter");
		String insertAfterText=insertAfter.getText();
		AddFieldRule rule=new AddFieldRule();
		rule.setProjectPath(projectPath);
		rule.setServerPojo(pojoNameText);
		rule.setServerMapper(mapperNameText);
		rule.setDatabase(database);
		rule.setTable(tableName);
		rule.setModule(moduleName);
		rule.setCurrModule(currModuleName);
		rule.setFieldName(fieldNameText);
		rule.setFieldComment(fieldCommentText);
		rule.setFieldType(fieldTypeText);
		if(ObjectUtils.isNotEmpty(fieldLengthText)) {
			rule.setFieldLength(Integer.parseInt(fieldLengthText));
		}
		if(ObjectUtils.isNotEmpty(fieldScaleText)) {
			rule.setFieldScale(Integer.parseInt(fieldScaleText));
		}
		rule.setFieldValue(fieldValueText);
		rule.setFieldDefault(fieldDefaultText);
		rule.setBasePackage(projectDomainText);
		rule.setInsertAfter(insertAfterText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_DOMAIN, projectDomainText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_POJO, pojoNameText);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_MAPPER, mapperNameText);
		try {
			String addFieldInfo=ProjectFactory.addField(rule);
			MessageFactory.info(QuickApplication.getWindow(), addFieldInfo);
		} catch (Exception e) {
			e.printStackTrace();
			MessageFactory.error(QuickApplication.getWindow(), "生成异常，消息："+e.getMessage());
		}
	}
	
	private static void genericsModule(){
		JComboBox<String> databaseSelect=QuickApplication.cache.get(QuickConstant.CACHE_MODULE_DATABASE);
		String database=(String)databaseSelect.getSelectedItem();
		if(ObjectUtils.isEmpty(database)){
			MessageFactory.error(QuickApplication.getWindow(), "未选定数据库");
			return ;
		}
		String projectPath = FileChooseFactory.getDefaultFile().getAbsolutePath()+"\\";
		String serverPojo = QuickApplication.getConfig(QuickConstant.CONF_PROJECT_POJO);
		File file=new File(projectPath+serverPojo);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Pojo路径“"+(projectPath+serverPojo)+"”不存在,生成失败");
			return ;	
		}
		String serverMapper = QuickApplication.getConfig(QuickConstant.CONF_PROJECT_MAPPER);
		file=new File(projectPath+serverMapper);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Mapper路径“"+(projectPath+serverMapper)+"”不存在,生成失败");
			return ;	
		}
		String serverAdapter = QuickApplication.getConfig(QuickConstant.CONF_PROJECT_ADAPTER);
		file=new File(projectPath+serverAdapter);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Adapter径“"+(projectPath+serverAdapter)+"”不存在,生成失败");
			return ;	
		}
		String serverExport = QuickApplication.getConfig(QuickConstant.CONF_PROJECT_EXPORT);
		file=new File(projectPath+serverExport);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目Export路径“"+(projectPath+serverExport)+"”不存在,生成失败");
			return ;	
		}
		
		Table table=QuickApplication.cache.get(QuickConstant.CACHE_MODULE_TABLE);
		int i=table.getSelectedRow();
		if(i<0){
			MessageFactory.error(QuickApplication.getWindow(), "未选定要生成的表");
			return ;
		}
		String server = (String)table.getValueAt(i, 3);
		if(ObjectUtils.isEmpty(server)){
			MessageFactory.error(QuickApplication.getWindow(), "请先设置所属服务");
			return ;
		}
		file=new File(projectPath+server);
		if(!file.exists()){
			MessageFactory.error(QuickApplication.getWindow(), "项目基础路径“"+(projectPath+server)+"”不存在,生成失败");
			return ;	
		}
		String module=(String)table.getValueAt(i, 4);
		if(ObjectUtils.isEmpty(module)){
			MessageFactory.error(QuickApplication.getWindow(), "请输入表的“所属模块”");
			return ;
		}
		String businessModule=(String)table.getValueAt(i, 5);
		if(ObjectUtils.isEmpty(businessModule)){
			MessageFactory.error(QuickApplication.getWindow(), "请输入表的“业务子模块”");
			return ;
		}
		String currModule=(String)table.getValueAt(i, 6);
		if(ObjectUtils.isEmpty(currModule)){
			MessageFactory.error(QuickApplication.getWindow(), "请输入表的“数据子模块”");
			return ;
		}
		String genericsModule=(String)table.getValueAt(i, 7);
		if(ObjectUtils.isEmpty(genericsModule)){
			MessageFactory.error(QuickApplication.getWindow(), "请输入表的“生成模块”");
			return ;
		}
		String dataOperator=(String)table.getValueAt(i, 8);
		if(ObjectUtils.isEmpty(dataOperator)){
			MessageFactory.error(QuickApplication.getWindow(), "请输入表的“功能”");
			return ;
		}
		
		ModuleRule rule=new ModuleRule();
		rule.setDatabase(database);
		rule.setTable((String)table.getValueAt(i, 1));
		rule.setTableComment((String)table.getValueAt(i, 2));
		rule.setModule(module);
		rule.setBusinessModule(businessModule);
		rule.setCurrModule(currModule);
		rule.setGenericsModuleList(ObjectUtils.asList(genericsModule.split("\\,")));
		rule.setDataOperator(dataOperator);
		rule.setProjectPath(projectPath);
		rule.setBasePackage(QuickApplication.getConfig(QuickConstant.CONF_PROJECT_DOMAIN));
		rule.setServerPojo(serverPojo);
		rule.setServerMapper(serverMapper);
		rule.setServerAdapter(serverAdapter);
		rule.setServer(server);
		rule.setServerExport(serverExport);
		QuickApplication.setConfig(QuickConstant.CONF_POJO_TABLE_GENERICS_CONFIG+database+"."+rule.getTable()+".module", module);
		QuickApplication.setConfig(QuickConstant.CONF_POJO_TABLE_GENERICS_CONFIG+database+"."+rule.getTable()+".currModule", currModule);
		QuickApplication.setConfig(QuickConstant.CONF_POJO_TABLE_GENERICS_CONFIG+database+"."+rule.getTable()+".businessModule", businessModule);
		QuickApplication.setConfig(QuickConstant.CONF_POJO_TABLE_GENERICS_CONFIG+database+"."+rule.getTable()+".genericsModule", genericsModule);
		QuickApplication.setConfig(QuickConstant.CONF_POJO_TABLE_GENERICS_CONFIG+database+"."+rule.getTable()+".dataOperator", dataOperator);
		QuickApplication.setConfig(QuickConstant.CONF_POJO_TABLE_GENERICS_CONFIG+database+"."+rule.getTable()+".server", server);
		try {
			String genericsFilePaths=ProjectFactory.genericsModule(rule);
			MessageFactory.info(QuickApplication.getWindow(), "生成完成，生成文件如下："+FileUtils.nextLine()+FileUtils.nextLine()+genericsFilePaths);
		} catch (Exception e) {
			e.printStackTrace();
			MessageFactory.error(QuickApplication.getWindow(), "生成异常，消息："+e.getMessage());
		}
	}
	
	private static void genericsServer(){
		String projectPath=((JTextField)QuickApplication.cache.getHash("server", "projectPath")).getText();
		if(ObjectUtils.isEmpty(projectPath)) {
			MessageFactory.error(QuickApplication.getWindow(), "项目路径不能为空");
			return ;
		}
		String projectDomain=((JTextField)QuickApplication.cache.getHash("server", "projectDomain")).getText();
		if(ObjectUtils.isEmpty(projectDomain)) {
			MessageFactory.error(QuickApplication.getWindow(), "项目域名不能为空");
			return ;
		}
		if(!projectDomain.matches("^[a-z]{2,}[a-z\\-0-9\\.]+$")) {
			MessageFactory.error(QuickApplication.getWindow(), "项目域名只能是小写字母、数字、-和.的组合，因为大写影响美观，请慎重");
			return ;
		}
		String nacosServer=((JTextField)QuickApplication.cache.getHash("server", "nacosServer")).getText();
		if(ObjectUtils.isEmpty(nacosServer)) {
			MessageFactory.error(QuickApplication.getWindow(), "Nacos服务器地址不能为空");
			return ;
		}
		String nacosNamespace=((JTextField)QuickApplication.cache.getHash("server", "nacosNamespace")).getText();
		if(ObjectUtils.isEmpty(nacosNamespace)) {
			MessageFactory.error(QuickApplication.getWindow(), "Nacos命名空间不能为空");
			return ;
		}
		String serverFileName=((JTextField)QuickApplication.cache.getHash("server", "serverFileName")).getText();
		if(ObjectUtils.isEmpty(serverFileName)) {
			MessageFactory.error(QuickApplication.getWindow(), "微服务文件名不能为空");
			return ;
		}
		String serverName=((JTextField)QuickApplication.cache.getHash("server", "serverName")).getText();
		if(ObjectUtils.isEmpty(serverName)) {
			MessageFactory.error(QuickApplication.getWindow(), "微服务名称不能为空");
			return ;
		}
		String serverPort=((JTextField)QuickApplication.cache.getHash("server", "serverPort")).getText();
		if(ObjectUtils.isEmpty(serverPort)) {
			MessageFactory.error(QuickApplication.getWindow(), "微服务端口不能为空");
			return ;
		}
		String serverComment=((JTextField)QuickApplication.cache.getHash("server", "serverComment")).getText();
		if(ObjectUtils.isEmpty(serverComment)) {
			MessageFactory.error(QuickApplication.getWindow(), "微服务中文名称不能为空");
			return ;
		}
		String username=((JTextField)QuickApplication.cache.getHash("server", "username")).getText();
		if(ObjectUtils.isEmpty(username)) {
			MessageFactory.error(QuickApplication.getWindow(), "作者不能为空");
			return ;
		}
		ServerRule rule=new ServerRule();
		rule.setProjectPath(projectPath);
		rule.setProjectDomain(projectDomain);
		rule.setNacosServer(nacosServer);
		rule.setNacosNamespace(nacosNamespace);
		rule.setServerFileName(serverFileName);
		rule.setServerName(serverName);
		rule.setServerClassName(StringUtils.toFirstUpper(serverName));
		rule.setServerPort(serverPort);
		rule.setServerComment(serverComment);
		rule.setUsername(username);
		rule.setDate(DateUtils.format("yyyy-MM-dd"));
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_DOMAIN, projectDomain);
		QuickApplication.setConfig(QuickConstant.CONF_NACOS_SERVER, nacosServer);
		QuickApplication.setConfig(QuickConstant.CONF_NACOS_NAMESPACE, nacosNamespace);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_SERVER_FILE_NAME, serverFileName);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_SERVER_NAME, serverName);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_SERVER_PORT, serverPort);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_SERVER_COMMENT, serverComment);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_AUTHOR, username);
		try {
			ProjectFactory.genericsServer(rule);
			MessageFactory.info(QuickApplication.getWindow(), "生成微服务完成");
		} catch (Exception e) {
			e.printStackTrace();
			MessageFactory.error(QuickApplication.getWindow(), "生成微服务失败，消息："+e.getMessage());
		}
		
	}
	
	@SuppressWarnings("unchecked")
	private static void genericsProject() {
		String projectName=((JTextField)QuickApplication.cache.getHash("createProject", "projectName")).getText();
		if(ObjectUtils.isEmpty(projectName)) {
			MessageFactory.error(QuickApplication.getWindow(), "项目名称不能为空");
			return ;
		}
		if(!projectName.matches("^[a-z]{2,}[a-z\\-0-9]+$")) {
			MessageFactory.error(QuickApplication.getWindow(), "项目名称只能是小写字母、数字和-的组合，因为大写影响美观，请慎重");
			return ;
		}
		String projectDomain=((JTextField)QuickApplication.cache.getHash("createProject", "projectDomain")).getText();
		if(ObjectUtils.isEmpty(projectDomain)) {
			MessageFactory.error(QuickApplication.getWindow(), "项目域名不能为空");
			return ;
		}
		if(!projectDomain.matches("^[a-z]{2,}[a-z\\-0-9\\.]+$")) {
			MessageFactory.error(QuickApplication.getWindow(), "项目域名只能是小写字母、数字、-和.的组合，因为大写影响美观，请慎重");
			return ;
		}
		String projectPath=((JTextField)QuickApplication.cache.getHash("createProject", "projectPath")).getText();
		if(ObjectUtils.isEmpty(projectPath)) {
			MessageFactory.error(QuickApplication.getWindow(), "项目路径不能为空");
			return ;
		}
		String projectDescribe=((JTextField)QuickApplication.cache.getHash("createProject", "projectDescribe")).getText();
		if(ObjectUtils.isEmpty(projectDescribe)) {
			MessageFactory.error(QuickApplication.getWindow(), "项目描述不能为空");
			return ;
		}
		String developTool=((JComboBox<String>)QuickApplication.cache.getHash("createProject", "developTools")).getSelectedItem().toString();
		String webFrameworks="layui";
		Component[] radioArray=QuickApplication.cache.getHash("createProject", "webFrameworks");
		for (Component radio : radioArray) {
			if(((JRadioButton)radio).isSelected()) {
				webFrameworks=((JRadioButton)radio).getText();
			}
		}
		ProjectConfig config=new ProjectConfig();
		config.setProjectName(projectName);
		config.setProjectDomain(projectDomain);
		config.setProjectPath(projectPath);
		config.setProjectDescribe(projectDescribe);
		config.setWebFramework(webFrameworks);
		config.setDevelopTool(developTool);
		QuickApplication.setConfig(QuickConstant.CONF_PROJECT_DOMAIN, projectDomain);
		ProjectFactory.generics(config);
	}
	
	private static void initProjectPath(){
		JTextField projectPath=((JTextField)QuickApplication.cache.getHash("createProject", "projectPath"));
		if(ObjectUtils.isEmpty(projectPath) || ObjectUtils.isEmpty(projectPath.getText())) {
			return ;
		}
		JTextField projectName=(JTextField)QuickApplication.cache.getHash("createProject", "projectName");
		if(ObjectUtils.isEmpty(projectName) || ObjectUtils.isEmpty(projectName.getText())) {
			return ;
		}
		projectPath.setText(projectPath.getText()+"\\"+projectName.getText());
	}
	
	private static void fileSelect(Object... comps) {
		File file=FileChooseFactory.create(
			Attribute.create()
			.put(Attribute.ATTRIBUTE_FILECHOOSE_DEPEND, comps[2])
			.put(Attribute.ATTRIBUTE_FILECHOOSE_TYPE, comps[1])
			.put(Attribute.ATTRIBUTE_FILECHOOSE_DEFAULT_FILEPATH, FileChooseFactory.getDefaultFile())
			.put(Attribute.ATTRIBUTE_FILECHOOSE_MULTISELECTION_ENABLED, false)
			.put(Attribute.ATTRIBUTE_FILECHOOSE_ENABLED, false)
			.put(Attribute.ATTRIBUTE_FILECHOOSE_FILTER, comps[0])
		);
		if(ObjectUtils.isNotEmpty(file)){
			if(!file.exists()){
				MessageFactory.error((JComponent)comps[0], "文件不存在");
				return ;
			}
			((JTextField)comps[3]).setText(file.getAbsolutePath());
			QuickApplication.setConfig(QuickConstant.CONF_FILE_CHOOSE_OPEN_PATH, file.isDirectory()?file.getAbsolutePath():file.getParent());
		}
	}
	
}
